﻿Imports System.Collections.ObjectModel
Imports System.ComponentModel
Public Class ExpensesList
    Inherits ObservableCollection(Of ExpensesClass)
    Dim petrolDB As New IETrackerDataClassesDataContext
    Private Shared list As New ExpensesList

    Public Shared Function GetList() As ExpensesList
        Return list
    End Function

    Private Sub New()
        ' Make the constructor private, enforcing the 
        ' "factory" concept -- the only way to create
        ' an instance of this class is by calling
        ' the GetList method.
        AddItems()
    End Sub

    Public Shared Sub Reset()
        list.ClearItems()
        list.AddItems()
    End Sub

    Private Sub AddItems()
        Dim expenses = From trans In petrolDB.Transactions _
                          Join transType In petrolDB.TransactionTypes _
                          On trans.TransactionTypeID Equals transType.TransactionTypeID _
                          Join transTypeDesc In petrolDB.TransactionTypeDescriptions
                          On transType.TransactionTypeDescID Equals transTypeDesc.TransactionTypeDescID _
                          Join transDesc In petrolDB.TransactionDescriptions
                          On transType.TransactionDescID Equals transDesc.TransactionDescriptionID _
                          Where transType.TransactionTypeDescID = 2 _
                          Select trans, transType, transTypeDesc, transDesc Order By trans.TransactionDate Descending



        For Each pItem In expenses '.ToList '.OrderByDescending(Function(x) x.trans.TransactionDate)
            With pItem
                Add(New ExpensesClass(.transDesc.TransactionDescription _
                                                , .trans.TransactionAmount _
                                                , .trans.TransactionDate _
                                                , .trans.TransactionID _
                                                , .transType.TransactionDescID _
                                                , .transType.OneOff _
                                                , .trans.Comment))
            End With

        Next
    End Sub
End Class
